package api

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"service_base_shop_api/order_web/global"
	protopb "service_base_shop_api/order_web/proto"
	"service_base_shop_api/order_web/utils"
	"strconv"
)

func GetOrderInfo(ctx *gin.Context)  {
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	GetOrderInfo,err := global.OrderClient.GetOrderInfo(context.Background(),&protopb.GetOrderInfoReq{
		UserId: int64(user_id),
	})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	orderInfo := gin.H{"pay_num":GetOrderInfo.PayNum,"rec_num":GetOrderInfo.RecNum,"refund_num":GetOrderInfo.RefundNum,"finish_num":GetOrderInfo.FinishNum}
	ctx.JSON(http.StatusOK,gin.H{"orderInfo":orderInfo,"status":1})

}

func GetOrderList(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"page","order_type"})
	if parms == nil {
		return
	}
	page,_ :=strconv.Atoi(parms["page"])
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	GetOrderList,err := global.OrderClient.GetOrderList(context.Background(),&protopb.GetOrderListReq{
		UserId: int64(user_id),
		Page: int64(page),
		OrderType: parms["order_type"],
	})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	orderInfo := []gin.H{}
	err = json.Unmarshal([]byte(GetOrderList.OrderList),&orderInfo)
	ctx.JSON(http.StatusOK,gin.H{"ord":orderInfo,"eachpage":GetOrderList.EachPage,"status":1})

}

func GetOrderDetails(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"order_id"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	order_id := utils.StrToInt64(parms["order_id"])
	details,err := global.OrderClient.GetOrderDetails(context.Background(),&protopb.GetOrderDetailsReq{UserId: int64(user_id),OrderId: order_id})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	ctx.JSON(http.StatusOK,gin.H{"ord":details.Order,"pro":details.Details,"status":1})
}

func GetOrderRefunds(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"page"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	page := utils.StrToInt64(parms["page"])
	refund,err := global.OrderClient.GetOrderRefundLists(context.Background(),&protopb.GetOrderRefundReq{UserId: int64(user_id),Page: page})
	fmt.Println(refund,err)
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	ctx.JSON(http.StatusOK,gin.H{"ord":refund.List,"status":1})
}

func BuyCart(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"cart_id"})
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	data,err := global.OrderClient.BuyCart(context.Background(),&protopb.BuyCartReq{CartId: parms["cart_id"],UserId: int64(user_id)})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
	}
	pro := []map[string]interface{}{}
	vou := []map[string]interface{}{}
	adds := map[string]interface{}{}
	yun := map[string]interface{}{}
	json.Unmarshal([]byte(data.Pro),&pro)
	json.Unmarshal([]byte(data.Vou),&vou)
	json.Unmarshal([]byte(data.Adds),&adds)
	json.Unmarshal([]byte(data.Yun),&yun)
	ctx.JSON(http.StatusOK,gin.H{"pro":pro,"vou":vou,"adds":adds,"yun":yun,"price":data.Price,"addemt":data.Addemt,"status":1})
}

func CreateOrder(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"cart_id","type","aid","remark","price","vid"})
	if parms == nil {
		return
	}
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	user_id := id.(uint64)
	vid := utils.StrToInt64(parms["vid"])
	aid := utils.StrToInt64(parms["aid"])
	price,_ := strconv.ParseFloat(parms["price"],2)
	data,err:= global.OrderClient.CreateOrder(context.Background(),&protopb.CreateOrderReq{Vid: vid,UserId: int64(user_id),Aid: aid,Remark: parms["remark"],Price: price,Type: parms["type"],CartId: parms["cart_id"]})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	arr := map[string]interface{}{
		"order_id":data.OrderId,
		"order_sn":data.OrderSn,
		"pay_type":data.PayType,
	}
	ctx.JSON(http.StatusOK,gin.H{"arr":arr,"status":1})
}

func OrderEdit(ctx *gin.Context)  {
	parms := utils.Requests(ctx,[]string{"id","type"})
	if parms == nil {
		return
	}
	id,ok := ctx.Get("ID")
	if !ok {
		ctx.JSON(http.StatusNotFound,gin.H{"status":0,"msg":"非法请求"})
	}
	back_remark := ctx.DefaultPostForm("back_remark","")
	user_id := id.(uint64)
	order_id := utils.StrToInt64(parms["id"])
	_,err:= global.OrderClient.OrderEdit(context.Background(),&protopb.OrderEditReq{UserId: int64(user_id),OrdrId: order_id,Otype: parms["type"],BackRemark: back_remark})
	if err != nil {
		utils.HandleGrpcErrorToHttp(err,ctx)
		return
	}
	ctx.JSON(http.StatusOK,gin.H{"status":1})
}